package com.easyOrder.service.requisition;

import com.easyOrder.core.comm.BeanMapper;
import com.easyOrder.core.utils.WebUtils;
import com.easyOrder.dao.RequisitionDetailMapper;
import com.easyOrder.dto.RequisitionDetailDTO;
import com.easyOrder.model.RequisitionDetail;
import com.easyOrder.model.RequisitionDetailExample;
import com.easyOrder.service.enums.RequisitionDetailState;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by liutao on 2017/7/21.
 */
@Service
public class RequisitionDetailService {

    @Autowired
    private RequisitionDetailMapper requisitionDetailMapper;

    /**
     * 查询下单的商品
     * @param requisitionId
     * @return
     */
    public List<RequisitionDetail> findOrder(Long requisitionId){
        RequisitionDetailExample example = new RequisitionDetailExample();
        example.createCriteria().andDocumentIdEqualTo(requisitionId).andStatusIdEqualTo(RequisitionDetailState.order.getValue());
        List<RequisitionDetail> requisitionDetails = requisitionDetailMapper.selectByExample(example);
        return requisitionDetails;
    }

    /**
     * 通过主键查询明细单
     * @param id
     * @return
     */
    public RequisitionDetail findById(Long id){
        RequisitionDetail requisitionDetail = requisitionDetailMapper.selectByPrimaryKey(id);
        return requisitionDetail;
    }

    /**
     * 修改明细
     * @param id
     * @param state
     */
    public void updateRequisitionDetail(Long id,Integer state){
        RequisitionDetail requisitionDetail = new RequisitionDetail();
        requisitionDetail.setReceiverId(WebUtils.Session.get().getUserId());
        requisitionDetail.setReceiverName(WebUtils.Session.get().getUserName());
        requisitionDetail.setStatusId(state);
        requisitionDetail.setId(id);
        requisitionDetailMapper.updateByPrimaryKeySelective(requisitionDetail);
    }

    /**
     * 通过状态查询明细数量
     * @param requisitionId 主单id
     * @param state 状态
     */
    public int findByStateCount(Long requisitionId,Integer state){
        RequisitionDetailExample example = new RequisitionDetailExample();
        example.createCriteria().andDocumentIdEqualTo(requisitionId).andStatusIdEqualTo(state);
        int i = requisitionDetailMapper.countByExample(example);
        return i;
    }

    /**
     * 通过状态查询明细数量
     * @param requisitionId 主单id
     */
    public int findByStateCount(Long requisitionId,List<Integer> states){
        RequisitionDetailExample example = new RequisitionDetailExample();
        example.createCriteria().andDocumentIdEqualTo(requisitionId).andStatusIdIn(states);
        int i = requisitionDetailMapper.countByExample(example);
        return i;
    }

    /**
     * 通过id获取明细信息
     * @param requisitionId
     * @return
     */
    public List<RequisitionDetailDTO> getRequisitionDetailDTOS(Long requisitionId) {
        RequisitionDetailExample example = new RequisitionDetailExample();
        example.createCriteria().andDocumentIdEqualTo(requisitionId);
        List<RequisitionDetail> requisitionDetails = requisitionDetailMapper.selectByExample(example);

        List<RequisitionDetailDTO> requisitionDetailDTOS = BeanMapper.mapList(requisitionDetails, RequisitionDetailDTO.class);
        if (CollectionUtils.isNotEmpty(requisitionDetailDTOS)) {
            requisitionDetailDTOS.forEach(requisitionDetailDTO -> {
                if (WebUtils.Session.get().getUserId().equals(requisitionDetailDTO.getReceiverId())) {
                    requisitionDetailDTO.setMe(true);
                }
                requisitionDetailDTO.setStatusStr(RequisitionDetailState.parse(requisitionDetailDTO.getStatusId()).getDesc());
            });
        }
        return requisitionDetailDTOS;
    }


}
